package com.maila.xiaoaoschool.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.maila.xiaoaoschool.widget.gridpasswordview.GridPasswordView;
import com.maila.xiaoaoschool.widget.gridpasswordview.imebugfixer.ImeDelBugFixedEditText;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 支付界面
 * Created by Administrator on 2017/10/17.
 */

public class PayActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.total_youhui)
    TextView totalYouhui;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.rb_usable)
    ImageView rbUsable;
    @BindView(R.id.rb_weixin)
    ImageView rbWeixin;
    @BindView(R.id.rb_zhifubao)
    ImageView rbZhifubao;
    @BindView(R.id.submit_order_btn)
    Button submitOrderBtn;
    private PopupWindow pwdpopupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        ButterKnife.bind(this);
    }

 void    resetrb(){
     rbZhifubao.setSelected(false);
     rbUsable.setSelected(false);
     rbWeixin.setSelected(false);
 }
    @OnClick({R.id.white_return_iv, R.id.rb_usable, R.id.rb_weixin, R.id.rb_zhifubao, R.id.submit_order_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.rb_usable:
                resetrb();
                rbUsable.setSelected(true);
                break;
            case R.id.rb_weixin:
                resetrb();
                rbWeixin.setSelected(true);
                break;
            case R.id.rb_zhifubao:
                resetrb();
                rbZhifubao.setSelected(true);
                break;
            case R.id.submit_order_btn:
                showPassWordPopupWindow();
                break;
        }
    }

    private void showPassWordPopupWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.password_popupwindow, null);
        View mParentView = LayoutInflater.from(this).inflate(R.layout.activity_pay, null);
        TextView commodity_price_tv = (TextView) contentView.findViewById(R.id.commodity_price_tv);
        ImageView iv_dismiss = (ImageView) contentView.findViewById(R.id.iv_dismiss);
        final GridPasswordView pswView = (GridPasswordView) contentView.findViewById(R.id.pswView);
        commodity_price_tv.setText("10.00");
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pwdpopupWindow.dismiss();
            }
        });
        ImeDelBugFixedEditText mInputView = pswView.getmInputView();
        mInputView.setFocusable(true);
        mInputView.setFocusableInTouchMode(true);
        mInputView.requestFocus();
        InputMethodManager inputManager =
                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(mInputView, InputMethodManager.RESULT_SHOWN);
        inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

        pswView.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(final String psw) {
                if (psw.length() == 6) {
                   pswView.clearPassword();
                    StartActivityUtils.start(PayActivity.this,ExpressProgressActivity.class);
                    finish();
                }
            }

            @Override
            public void onInputFinish(String psw) {

            }
        });
        pwdpopupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        pwdpopupWindow.setTouchable(true);
        pwdpopupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
        pwdpopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                popupWindow.dismiss();
            }
        });
        pwdpopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popupwindow_shape));
        pwdpopupWindow.showAtLocation(mParentView, Gravity.NO_GRAVITY, 0, 0);
        pwdpopupWindow.setOutsideTouchable(true);
    }
}
